/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ib.algokit.forex;

import static org.activequant.util.tools.IdentityUtils.safeCompare;

import java.util.Comparator;

import org.activequant.core.types.TimeStamp;

import com.ib.algokit.InstrumentAnalyzer;
import com.ib.algokit.history.ProfitLimitClosedEvent;
import com.ib.algokit.history.StopLossClosedEvent;
/**
 *
 * @author dmisev
 */
public class ForexPairStateRangesComparator implements Comparator<ForexPairStateRanges>{
	public int compare(ForexPairStateRanges o1, ForexPairStateRanges o2) {
		int rc;
		TimeStamp o1OpeningTimeStamp=null;
		TimeStamp o1ClosingTimeStamp=null;
		TimeStamp o2OpeningTimeStamp=null;
		TimeStamp o2ClosingTimeStamp=null;
		if(o1.getLongLossOutcomes()!=null&&!o1.getLongLossOutcomes().isEmpty()){
			StopLossClosedEvent stopLossClosedEvent=o1.getLongLossOutcomes().iterator().next();
			o1OpeningTimeStamp=stopLossClosedEvent.getOpeningTimeStamp();
			o1ClosingTimeStamp=stopLossClosedEvent.getEventTimeStamp();
		}else if(o1.getShortLossOutcomes()!=null&&!o1.getShortLossOutcomes().isEmpty()){
			StopLossClosedEvent stopLossClosedEvent=o1.getShortLossOutcomes().iterator().next();
			o1OpeningTimeStamp=stopLossClosedEvent.getOpeningTimeStamp();
			o1ClosingTimeStamp=stopLossClosedEvent.getEventTimeStamp();
		}else if(o1.getLongProfitOutcomes()!=null&&!o1.getLongProfitOutcomes().isEmpty()){
			ProfitLimitClosedEvent profitLimitClosedEvent=o1.getLongProfitOutcomes().iterator().next();
			o1OpeningTimeStamp=profitLimitClosedEvent.getOpeningTimeStamp();
			o1ClosingTimeStamp=profitLimitClosedEvent.getEventTimeStamp();
		}else if(o1.getShortProfitOutcomes()!=null&&!o1.getShortProfitOutcomes().isEmpty()){
			ProfitLimitClosedEvent profitLimitClosedEvent=o1.getShortProfitOutcomes().iterator().next();
			o1OpeningTimeStamp=profitLimitClosedEvent.getOpeningTimeStamp();
			o1ClosingTimeStamp=profitLimitClosedEvent.getEventTimeStamp();
		}

		if(o2.getLongLossOutcomes()!=null&&!o2.getLongLossOutcomes().isEmpty()){
			StopLossClosedEvent stopLossClosedEvent=o2.getLongLossOutcomes().iterator().next();
			o2OpeningTimeStamp=stopLossClosedEvent.getOpeningTimeStamp();
			o2ClosingTimeStamp=stopLossClosedEvent.getEventTimeStamp();
		}else if(o2.getShortLossOutcomes()!=null&&!o2.getShortLossOutcomes().isEmpty()){
			StopLossClosedEvent stopLossClosedEvent=o2.getShortLossOutcomes().iterator().next();
			o2OpeningTimeStamp=stopLossClosedEvent.getOpeningTimeStamp();
			o2ClosingTimeStamp=stopLossClosedEvent.getEventTimeStamp();
		}else if(o2.getLongProfitOutcomes()!=null&&!o2.getLongProfitOutcomes().isEmpty()){
			ProfitLimitClosedEvent profitLimitClosedEvent=o2.getLongProfitOutcomes().iterator().next();
			o2OpeningTimeStamp=profitLimitClosedEvent.getOpeningTimeStamp();
			o2ClosingTimeStamp=profitLimitClosedEvent.getEventTimeStamp();
		}else if(o2.getShortProfitOutcomes()!=null&&!o2.getShortProfitOutcomes().isEmpty()){
			ProfitLimitClosedEvent profitLimitClosedEvent=o2.getShortProfitOutcomes().iterator().next();
			o2OpeningTimeStamp=profitLimitClosedEvent.getOpeningTimeStamp();
			o2ClosingTimeStamp=profitLimitClosedEvent.getEventTimeStamp();
		}

		long openingTime1=o1OpeningTimeStamp.getNanoseconds()/InstrumentAnalyzer.ONE_SECOND;//a second
		long openingTime2=o2OpeningTimeStamp.getNanoseconds()/InstrumentAnalyzer.ONE_SECOND;//a second
		rc = safeCompare(openingTime1, openingTime2);
		if (rc != 0) return rc;
		//long prevTime1=o1.getPreviousScanTime().getNanoseconds();
		//long prevTime2=o2.getPreviousScanTime().getNanoseconds();
		rc = safeCompare(o1ClosingTimeStamp.getNanoseconds(), o2ClosingTimeStamp.getNanoseconds());
		//if (rc != 0) return rc;
		//rc = safeCompare(o1.getPreviousScanTime(), o2.getPreviousScanTime());
		//if (rc != 0) return rc;
		//rc = safeCompare(o1.getChangeInterval(), o2.getChangeInterval());
		//if (rc != 0) return rc;
		return rc;
	}
}
